﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using OMS.OMSService;
using OMS.Models;
using PagedList;

namespace OMS.Controllers
{
    public class SupervisorController : Controller
    {
        //
        // GET: /Supervisor/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddItem()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddItem(ItemModelView itemModelView)
        {
            bool result = false, resultSpec = false;
            if (ModelState.IsValid)
            {
                Service1 service = new Service1();
                ItemModel itemModel = itemModelView.ToItemModel();
                itemModel.IdSpecified = true;
                itemModel.PriceSpecified = true;
                itemModel.QuantitySpecified = true;
                service.CreateItem(itemModel, out result, out resultSpec);
            }
            if (result)
                return RedirectToAction("GetAllItems");
            else
                return RedirectToAction("AddItem"); //TODO: RedirectToArror
        }

        public ActionResult GetItem(int id)
        {
            //TODO: idspecified
            Service1 service = new Service1();
            ItemModelView itemModelView = new ItemModelView(service.GetItemById(id, true));
            return View(itemModelView);
        }

        public ActionResult GetAllItems(int? page)
        {
            Service1 service = new Service1();
            List<ItemModel> items = service.GetAllItems().ToList();
            ViewBag.TotalItems = items.Count;
            List<ItemModelView> itemModelViews = new List<ItemModelView>();
            foreach (var item in items)
            {
                itemModelViews.Add(new ItemModelView(item));
            }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            //return View(itemModelViews);
            return View(itemModelViews.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult EditItem(int id)
        {
            Service1 service = new Service1();
            ItemModelView itemModelView = new ItemModelView(service.GetItemById(id, true));
            return View(itemModelView);
        }

        [HttpPost]
        public ActionResult EditItem(ItemModelView itemModelView)
        {
            bool result = false, resultSpecified = false;
            ItemModel itemModel = itemModelView.ToItemModel();
            if (ModelState.IsValid)
            {
                Service1 service = new Service1();
                itemModel.IdSpecified = true;
                itemModel.PriceSpecified = true;
                itemModel.QuantitySpecified = true;
                service.EditItem(itemModel, out result, out resultSpecified);
            }
            if (result)
                return RedirectToAction("GetAllItems");
            else
                return RedirectToAction("EditItem");    //RedirectToArror
        }

        public ActionResult DeleteItem(int id)
        {
            Service1 service = new Service1();
            ItemModelView itemModelView = new ItemModelView(service.GetItemById(id, true));
            return View(itemModelView);
        }

        [HttpPost]
        public ActionResult DeleteItem(FormCollection form)
        {
            bool result = false, resultSpec = false;
            Service1 service = new Service1();
            int id = Int32.Parse(form["Id"]);
            service.DeleteItem(id, true, out result, out resultSpec);
            if (result)
                return RedirectToAction("GetAllItems");
            else
                return RedirectToAction("DeleteItem", id);
        }
    }
}
